package Dao;

import model.Student;
import model.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import util.C3P0Util;
import util.DBHelper;
import util.Md5Util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDao {
    public String checkLogin(User user){
        String sql = "select ustuno from user where username=? and password=?";
        String stuno="";
        QueryRunner query = new QueryRunner(C3P0Util.getConnection());
        Map<String,Object> map = null;
        try {
            map = query.query(sql,new MapHandler(),new Object[]{user.getUserName(), Md5Util.stringToMD5(user.getPassword())});
            stuno = (String)map.get("ustuno");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stuno;
    }


//    public boolean checkLogin(User user){
//        String sql = "select count(*) as count from user where username=? and password=?";
//        long count=0;
//        QueryRunner query = new QueryRunner(C3P0Util.getConnection());
//        Map<String,Object> map = null;
//        try {
//            map = query.query(sql,new MapHandler(),new Object[]{user.getUserName(), Md5Util.stringToMD5(user.getPassword())});
//            count = (Long)map.get("count");
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return count>0;
//    }

    public boolean saveUser(User u){
        String sql = "insert into user(username,password) values(?,?)";
        QueryRunner query = new QueryRunner(C3P0Util.getConnection());
        int result=0;

        try {
            result = query.update(sql,new Object[]{ u.getUserName(),Md5Util.stringToMD5(u.getPassword())});
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result>0;
    }

    public int setStuNo(String userName,String stuNo){
        String sql="update user set ustuno=? where username=?";
        QueryRunner queryRunner=new QueryRunner(C3P0Util.getConnection());
        int result;
        try {
            result=queryRunner.update(sql,new Object[]{stuNo,userName});
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }


    public int deleteUser(String username){
        String sql = "delete from user where username=?";
        QueryRunner query = new QueryRunner(C3P0Util.getConnection());
        try {
            int result=query.update(sql,username);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getIdByStuNo(String stuNo) {
        String sql = "select username from user where ustuno=?";
        String username="";
        QueryRunner query = new QueryRunner(C3P0Util.getConnection());
        Map<String,Object> map = null;
        try {
           // map = query.query(sql,new MapHandler(),new Object[]{stuNo});
            map = query.query(sql,new MapHandler(),stuNo);
            username = (String)map.get("username");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return username;
    }

}
